package com.hnx.admin.aurora.system.service.impl;

import com.hnx.admin.aurora.annotation.DataScope;
import com.hnx.admin.aurora.annotation.Pager;
import com.hnx.admin.aurora.security.context.AuthenticationContextHolder;
import com.hnx.admin.aurora.system.domain.dto.loginInfo.SysLogininfoDTO;
import com.hnx.admin.aurora.system.domain.param.loginInfor.form.LoginInfoForm;
import com.hnx.admin.aurora.system.domain.param.loginInfor.query.LoginInfoQuery;
import com.hnx.admin.aurora.system.domain.vo.emp.entity.EmpVO;
import com.hnx.admin.aurora.system.domain.vo.user.entity.UserDetailVO;
import com.hnx.admin.aurora.system.mapper.loginInfor.LoginInfoMapper;
import com.hnx.admin.aurora.system.service.AsyncLoginLogWriter;
import com.hnx.admin.aurora.system.service.LoginInfoService;
import com.hnx.admin.aurora.utils.IPUtil;
import com.hnx.admin.aurora.utils.ServletUtil;
import com.hnx.admin.aurora.web.json.ResultJson;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/*
创建人： 倔强的头发
创建时间：2025/6/17
功能描述:

*/
@Service
@RequiredArgsConstructor
public class LoginInfoServiceImpl implements LoginInfoService {
    private final LoginInfoMapper loginInfoMapper;
    private final AsyncLoginLogWriter asyncLoginLogWriter;
    @Override
    public void writeLog(String username, Integer status, String msg) {
        HttpServletRequest request = ServletUtil.getRequest();
        if (Objects.nonNull(request)) {
            final UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
            String os = userAgent.getOperatingSystem().getName();
            // 获取客户端浏览器
            String browser = userAgent.getBrowser().getName();
            String ip = IPUtil.getClientIp(request);
            LoginInfoForm sysLogininfor = LoginInfoForm.builder()
                    .browser(browser)
                    .os(os)
                    .msg(msg)
                    .status(status)
                    .ipaddr(ip)
                    .userName(username)
                    .build();
            if (status == 0) {
                Authentication auth = AuthenticationContextHolder.getContext();
                UserDetailVO userDetailVO = (UserDetailVO) auth.getPrincipal();
                if (Objects.nonNull(userDetailVO)) {
                    EmpVO user = userDetailVO.getUserVO().getEmp();
                    if(Objects.nonNull(user)) {
                        sysLogininfor.setUserId(user.getUserId());
                        sysLogininfor.setDeptId(user.getDeptId());
                    }
                }
            }
            asyncLoginLogWriter.insertLogininfor(sysLogininfor);
//            loginInfoMapper.insertLogininfor(sysLogininfor);
            AuthenticationContextHolder.clearContext();
        }
    }

    @Override
    @DataScope
    @Pager(orderBy = " info_id desc")
    public ResultJson selectLogininforList(LoginInfoQuery logininfor) {
        List<SysLogininfoDTO> list = loginInfoMapper.selectLogininforList(logininfor);
        return ResultJson.success().add("rows", list);
    }

    @Override
    public ResultJson cleanLogininfor() {
        return null;
    }
}
